package com.ayg.controller;


import com.ayg.pojo.User;
import com.ayg.service.UserService;
import com.ayg.vo.UserResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@CrossOrigin  //跨域资源共享
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册账户
     * URL: /user/register
     * @param user   对象
     * @return  返回UserResult对象
     */

    @PostMapping("/register")
    public UserResult createAccount(@RequestBody User user) {
        String StatusCode = userService.createAccount(user);
        if (StatusCode.equals("2001")) {
            return UserResult.StatusCode201();
        }else if(StatusCode.equals("2002")) {
            return UserResult.StatusCode202();
        }else if(StatusCode.equals("2003")){
            return UserResult.StatusCode203();
        }else if(StatusCode.equals("2004")){
            return UserResult.StatusCode204();
        }else{
            return UserResult.StatusCode205();
        }
    }

    /**
     * 需求: 根据用户名和密码实现用户登录,要求返回token
     * URL: /user/login
     * 请求参数: post  JSON {username:"xxx",password:"xxxx"}
     * 返回值: 返回UserResult对象 (token)
     */

    @PostMapping("/login")
    public UserResult login(@RequestBody User user){
        //执行后端登录操作,要求返回token密钥
        String token = userService.login(user);
        System.out.println(token);
        //判断token返回值是否有效

        if (token.equals("2006")){
            return UserResult.StatusCode206();
        }else if(token.equals("2003")){
            return UserResult.StatusCode203();
        }else if(token.equals("2007")){
            return UserResult.StatusCode207();
        }else{
            return UserResult.StatusCode208(token);//登录成功返回token
        }
    }

    /**
     * 激活账号
     * @param confirmCode   确认码
     * @return  返回UserResult对象
     */

    @GetMapping("/activation")
    public UserResult activationAccount(String confirmCode){
        String activation=userService.activationAccount(confirmCode);
        if (activation.equals("2009")){
            return UserResult.StatusCode209();
        }else if (activation.equals("2010")){
            return UserResult.StatusCode210();
        }else{
            return UserResult.StatusCode211();
        }

    }

    /**
     * 找回密码
     * @param user user对象
     * @return 返回UserResult对象
     */

    @PostMapping("/pass")
    public UserResult pass(@RequestBody User user){
      String resultPass=userService.updatePass(user);
        if (resultPass.equals("2012")){
            return UserResult.StatusCode212();
        }else if (resultPass.equals("2013")){
            return UserResult.StatusCode213();
        }else{
            return UserResult.StatusCode211();
        }
    }

    /**
     * 密码重置邮件确认
     * @param userEmail         邮箱
     * @param userPassword      密码
     * @return  返回UserResult对象
     */
    @GetMapping("/activationPass")
    public UserResult activationPassAccount(String userEmail, String userPassword){
        String activation=userService.activationPass(userEmail,userPassword);
        if (activation.equals("2009")){
            return UserResult.StatusCode209();
        }else if (activation.equals("2014")){
            return UserResult.StatusCode214();
        }else{
            return UserResult.StatusCode214();
        }

    }

}